package edu.hsbremen.teamscore.activities;

import java.util.Stack;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import edu.hsbremen.teamscore.R;
import edu.hsbremen.teamscore.events.TimerObserver;
import edu.hsbremen.teamscore.ui.UiCountdownTimer;

public class ScreenBasketball extends Activity {
	
	private Button pauseBtn;
	private Button undoBtn;
	private Button team1Pt1;
	private Button team1Pt2;
	private Button team1Pt3;
	private Button team2Pt1;
	private Button team2Pt2;
	private Button team2Pt3;
	
	private TextView score1;
	private TextView score2;
	private UiCountdownTimer countdownTimer;
	
	private TextView team1Name;
	private TextView team2Name;
	
	private TextView quarter;
	private int quarterNr = 1;
	
	private Stack<ScoreData> undoStack;
	private boolean timerRunning = false;
	
	private TimerObserver countdownObserver;
			
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.basketball);
		
		undoStack = new Stack<ScoreData>();
		
		View.OnClickListener clickListener = new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				onClickHandler(v);
			}			
		};
		
		pauseBtn = (Button) findViewById(R.id.basketball_pause_btn);
		undoBtn = (Button) findViewById(R.id.basketball_undo_btn);
		team1Pt1 = (Button) findViewById(R.id.basketball_team1_1pt);
		team1Pt2 = (Button) findViewById(R.id.basketball_team1_2pt);
		team1Pt3 = (Button) findViewById(R.id.basketball_team1_3pt);
		team2Pt1 = (Button) findViewById(R.id.basketball_team2_1pt);
		team2Pt2 = (Button) findViewById(R.id.basketball_team2_2pt);
		team2Pt3 = (Button) findViewById(R.id.basketball_team2_3pt);
		
		pauseBtn.setOnClickListener(clickListener);
		undoBtn.setOnClickListener(clickListener);
		team1Pt1.setOnClickListener(clickListener);
		team1Pt2.setOnClickListener(clickListener);
		team1Pt3.setOnClickListener(clickListener);
		team2Pt1.setOnClickListener(clickListener);
		team2Pt2.setOnClickListener(clickListener);
		team2Pt3.setOnClickListener(clickListener);		
		
		countdownTimer = (UiCountdownTimer) findViewById(R.id.basketball_timer);
		countdownObserver = new CountdownObserver(10, this);
		countdownTimer.addTimerEvent(countdownObserver);
		
		score1 = (TextView) findViewById(R.id.basketball_score_team1);
		score2 = (TextView) findViewById(R.id.basketball_score_team2);
		score1.setText("0");
		score2.setText("0");
		
		quarter = (TextView) findViewById(R.id.basketball_quarter);
		
		team1Name = (TextView) findViewById(R.id.basketball_name_team1);
		team2Name = (TextView) findViewById(R.id.basketball_name_team2);
		Bundle namesBundle = getIntent().getExtras();
		team1Name.setText(namesBundle.getString("team1"));
		team2Name.setText(namesBundle.getString("team2"));
	}
	
	private void onClickHandler(View view) {
		if(view.equals(pauseBtn)){
			if(!timerRunning){
				timerRunning = true;
				countdownTimer.startCountdown();
			}
			else{
				timerRunning = false;
				countdownTimer.stopCountdown();
			}
		}
		else if(view.equals(undoBtn)){
			undo();
		}
		else if(view.equals(team1Pt1)){
			setScore(score1, 1);
		}
		else if(view.equals(team1Pt2)){
			setScore(score1, 2);
		}
		else if(view.equals(team1Pt3)){
			setScore(score1, 3);
		}
		else if(view.equals(team2Pt1)){
			setScore(score2, 1);
		}
		else if(view.equals(team2Pt2)){
			setScore(score2, 2);
		}
		else if(view.equals(team2Pt3)){
			setScore(score2, 3);
		}
		
	}

	private void undo() {
		if(!undoStack.isEmpty()){
			ScoreData scoreData = undoStack.pop();
			int points = scoreData.getPoints();
			TextView view= (TextView) scoreData.getView();
			
			int currentPts = Integer.parseInt((String) view.getText());
			currentPts -= points;
			view.setText(String.valueOf(currentPts));			
		}		
		
	}

	private void setScore(TextView scoreView, int scorePts) {
		int currentPts = Integer.parseInt((String) scoreView.getText());
		currentPts += scorePts;
		scoreView.setText(String.valueOf(currentPts));
		
		undoStack.push(new ScoreData(scoreView, scorePts));		
	}
	
	public void setQuarter(){
		timerRunning = false;
		quarterNr++;
		if(quarterNr <=4){		
			quarter.setText(quarterNr + " / 4");
			
			countdownTimer.resetCountdown();
			timerRunning = false;
			countdownTimer.addTimerEvent(countdownObserver);
		}
		else{
			quarter.setTextColor(Color.rgb(255, 0, 0));
			quarter.setText("Game Over!");			
		}
	}
	
	//Helper class for the Score Data container
	private class ScoreData{
		private int points;
		private View view;
		public ScoreData(View view, int points){
			this.points = points;
			this.view = view;			
		}
		
		public View getView(){
			return view;
		}
		
		public int getPoints(){
			return points;
		}
	}
	
	//Countdown Timer Observer to count the quarters
	private class CountdownObserver implements TimerObserver{
		Activity activity;
		long time = 0;
		
		public CountdownObserver(long t, Activity act){
			this.activity = act;
			this.time = t;
		}

		@Override
		public long getMillisWhenShouldHappen() {
			return this.time;
		}

		@Override
		public void update() {
			((ScreenBasketball)this.activity).setQuarter();
		}

		@Override
		public Activity getActivity() {
			return this.activity;
		}
		
	}

}